<template>
  <view class="container">
    <!-- 卡号输入区域 -->
    <view class="input-container">
      <text class="label">{{ $t('machineCard.cardNumber') }}</text>
      <input 
        class="input-field" 
        placeholder="{{ $t('machineCard.inputCardNumber') }}" 
        v-model="cardNumber"
        placeholder-class="placeholder-style"
        type="text"
        :disabled="hasBound"
      />
    </view>

    <!-- 绑定/解绑按钮 -->
    <button class="bind-btn" @click="handleBind">{{ hasBound ? $t('machineCard.unbind') : $t('machineCard.confirmBind') }}</button>
  </view>
</template>

<script>
	
export default {
  data() {
    return {
      // 卡号输入值
      cardNumber: '',
      // 是否已绑定机修卡
      hasBound: false
    };
  },
  onLoad(options) {
    // 从URL参数中获取卡号
    const machineRepairCard = options.machineRepairCard || '';
    console.log('从URL参数获取到的机修卡号:', machineRepairCard);
    // 判断卡号是否存在
    if (machineRepairCard) {
      this.cardNumber = machineRepairCard;
      this.hasBound = true;
    }
  },
  mounted() {
  	uni.setNavigationBarTitle({
  		title: this.$t('title.bindMechanicCard')
  	});
  },
  methods: {
    // 处理绑定/解绑逻辑
    async handleBind() {
      if (!this.hasBound) {
        // 绑定操作
        if (!this.cardNumber.trim()) {
          uni.showToast({
            title: this.$t('machineCard.pleaseInputCard'),
            icon: 'none'
          });
          return;
        }
        
        try {
          const response = await this.$http.post('/functionCard/bindMachineRepairCard', {
            machineRepairCard: this.cardNumber
          });
          
          if (response === true) {
            uni.showToast({
              title: this.$t('machineCard.bindSuccess'),
              icon: 'success'
            });
            // 延迟关闭页面
            setTimeout(() => {
              uni.navigateBack();
            }, 1500);
          } else {
            uni.showToast({
              title: this.$t('machineCard.bindFailed'),
              icon: 'none'
            });
          }
        } catch (error) {
          console.error('绑定失败:', error);
          uni.showToast({
            title: this.$t('machineCard.networkError'),
            icon: 'none'
          });
        }
      } else {
        // 解绑操作
        try {
          const response = await this.$http.post('/functionCard/unbindMachineRepairCard', {});
          
          if (response === true) {
            uni.showToast({
              title: this.$t('machineCard.unbindSuccess'),
              icon: 'success'
            });
            // 延迟关闭页面
            setTimeout(() => {
              uni.navigateBack();
            }, 1500);
          } else {
            uni.showToast({
              title: this.$t('machineCard.unbindFailed'),
              icon: 'none'
            });
          }
        } catch (error) {
          console.error('解绑失败:', error);
          uni.showToast({
            title: '网络错误，请重试',
            icon: 'none'
          });
        }
      }
    }
  }
};
</script>

<style scoped>
.container {
  background-color: #f5f5f5;
  min-height: 100vh;
  padding: 40rpx 30rpx;
}

/* 输入框容器 */
.input-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #ffffff;
  padding: 30rpx 25rpx;
  border-radius: 8rpx;
  margin-bottom: 60rpx;
}

/* 标签样式 */
.label {
  font-size: 32rpx;
  color: #333333;
  width: 100rpx; /* 固定标签宽度 */
}

/* 输入框样式 */
.input-field {
  flex: 1;
  font-size: 32rpx;
  color: #333333;
  text-align: right; /* 输入内容靠右对齐 */
  height: 40rpx;
  padding: 0;
  margin: 0;
  border: none;
  outline: none;
}

/* 占位符样式 */
.placeholder-style {
  color: #cccccc;
  font-size: 32rpx;
}

/* 绑定按钮样式 */
.bind-btn {
  width: 100%;
  height: 100rpx;
  line-height: 100rpx;
  background-color: #ff3b30; /* 红色背景 */
  color: #ffffff;
  font-size: 34rpx;
  border-radius: 8rpx;
}
</style>